<!DOCTYPE html> 
<!--
 Copyright (c) 2010 - 2017 TU Wien ACIN, fortiss GmbH
  
 This program and the accompanying materials are made available under the
 terms of the Eclipse Public License 2.0 which is available at
 http://www.eclipse.org/legal/epl-2.0.

 SPDX-License-Identifier: EPL-2.0
 
 Contributors:
   Carolyn Oates, Monika Wenger, Alois Zoitl, Jose Cabral
     - initial API and implementation and/or initial documentation
-->

<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title>External Event SIFB</title>
	<link rel="stylesheet" type="text/css" href="../help.css">
</head>

<body>
<h1 id="topOfPage">External Event SIFB</h1>
<p>External Events are events in FORTE, which are hidden and can not be seen in 4DIAC. They do not appear as red event lines. They are usually used for Communication between Hardware and Function Blocks. E.g. <span class="element4diac">E_CYCLE</span> is a Function Block that uses this functionality. It registers to the <span class="element4diac">CTimerHandler</span> Class to get External Events in a periodic time.</p>

<h2>Recieving External Events</h2>
<p>This is an advanced example showing how to make your own timed T-Flip-Flop Service Interface Function Block. It is based on the <span class="element4diac">E_CYCLE</span> Block and works quite the same way. This tutorial shows you how to build it from scratch. Consider that this is just an example that does not follow the IEC 61499 standard.</p> 

<h3>The Interface</h3>
<p>T Flip-Flop as SIFB with integrated Timer. <a href="../../html/4diacIDE/createOwnTypes.html#exportTypes">Export the interface to FORTE</a> (*.h and *.cpp files) and <a href="../../html/installation/install.html#externalModules">include them to your build</a>.</p>
<img src="../../html/development/img/flipFlop_integratedTimer.jpg" alt="Interface of the T Flip-Flop as SIFB with integrated Timer"/>

<h3>Edit the Headerfile</h3>
<p>This time we want to create a timed <span class="element4diac">SIFB</span> so we do not inherit from <span class="element4diac">CFunctionBlock</span>. Instead we inherit from <span class="element4diac">CEventSourceFB</span>. A <span class="element4diac">CEventSourceFB</span> can react to External Events. Therefore you should correct the first line of your class declaration like this:</p> 

<div class="code">class FORTE_SIBF_T_FF3 : public CEventSourceFB{</div>

<p>In this case we want to get External Events from the included Timer handler. Include the 2 files <span class="fileLocation">timerha.h</span> and <span class="fileLocation">resource.h</span> and change <span class="fileLocation">funcbloc.h</span> to <span class="fileLocation">esfb.h</span>. Your include parameters should look like this:</p>

<div class="code">#include &lt;timerha.h&gt; //added
#include &lt;resource.h&gt; //added
#include &lt;esfb.h&gt; //instead of #include &#60;funcbloc.h&#62;
#include &lt;forte_time.h&gt;
#include &lt;forte_bool.h&gt;</div>

<p>Then we need a new Constuctor as well as two new private members <span class="specificText">bool m_bActive;</span> which will indicate that the timed Function Block is currently active and <span class="specificText">STimedFBListEntry m_stTimeListEntry;</span> which represents the Timer list entry of this timed Function Block.</p> 

<div class="code">EVENT_SOURCE_FUNCTION_BLOCK_CTOR(FORTE_SIBF_T_FF3){
	    m_stEventSourceEventEntry.m_poFB = this;
	    setEventChainExecutor(pa_poSrcRes-&gt;getResourceEventExecution());
	    m_bActive = false;
	    m_stTimeListEntry.m_stTimeOut.m_nLowerValue = 0;
	    m_stTimeListEntry.m_stTimeOut.m_nUpperValue = 0;
	    m_stTimeListEntry.m_nInterval = 0;
	    m_stTimeListEntry.m_pstNext = 0;
	    m_stTimeListEntry.m_poTimedFB = this;
	    m_stTimeListEntry.m_eType = e_Periodic;
};</div>

<p>Remove the default constructor <span class="specificText">FUNCTION_BLOCK_CTOR</span> and replace it with above. This consturctor registers this Function Block as a External Event Listener, that triggers this Function Block if an event occurs. Then the timer list entry is configured.</p>

<h3>Edit the Sourcefile</h3>
<p>Then you need to edit the <span class="specificText">executeEvent</span> method. The usual start and stop Events will register this block to the <span class="specificText">CTimerHandler</span>. Add a new event <span class="specificText">cg_nExternalEventID</span>, which is defined in <span class="specificText">CEventSourceFB</span>, and let it set your outputs. This will be triggered if your Function Block receives an External Event.</p>

<div class="code">void FORTE_SIBF_T_FF3::executeEvent(int pa_nEIID){
  switch(pa_nEIID){
	case cg_nExternalEventID:
		Q() = !Q();
	  sendOutputEvent(scm_nEventEOID);
	  break;
	case scm_nEventSTOPID:
	  if(m_bActive){
		CTimerHandler::sm_poFORTETimer-&gt;unregisterTimedFB(this);
		m_bActive = false;
	  }
	  break;
	case scm_nEventSTARTID:
	  if(!m_bActive){
		CTimerHandler::sm_poFORTETimer-&gt;registerTimedFB(&m_stTimeListEntry, DT());
		m_bActive = true;
	  }
	  break;
	default:
	  break;
  }
}</div>

<p>Compile FORTE and run it. Test your new Function Block with the tester. If something went wrong, compare your code with the following files.</p>

<h2>Sending External Events</h2>
<p>To send External Events your Class must inherit from <span class="specificText">CExternalEventHandler</span>. In the Constructor you must register this class:</p>
<div class="specificText">m_nExtEvHandID = sm_poDeviceExecution-&gt;registerExternalEventHandler(this);</div>

<p>Then you can send External Events with:</p>
<div class="specificText">if(sm_poDeviceExecution-&gt;extEvHandlerIsAllowed(m_nExtEvHandID)){
   sm_poDeviceExecution-&gt;startNewEventChain(pointerToTargetFB);
}</div>

<h1>Where to go from here?</h1>

<p>Go back to Development index:</p>

<p><a href="../../html/development/developmentIndex.html">Development Index</a></p>

<p>If you want to go back to the Start Here page, we leave you here a fast access</p>

<p><a href="../../html/startHere/startHere.html">Start Here page</a></p>

<p class="goToTop">Or <a href="#topOfPage">Go to top</a></p>

</body>
</html>
